package com.songcha.library_common.util

import java.util.Calendar
import java.util.Date

class DateUtil {
    companion object{
        @JvmStatic
        fun getYear(date:Date):Int{
            val calendar = Calendar.getInstance()
            calendar.time = date
            return getYear(calendar)
        }

        @JvmStatic
        fun getMonth(date:Date):Int{
            val calendar = Calendar.getInstance()
            calendar.time = date
            return getMonth(calendar)
        }

        @JvmStatic
        fun getDay(date:Date):Int{
            val calendar = Calendar.getInstance()
            calendar.time = date
            return getDay(calendar)
        }

        @JvmStatic
        fun getYear(calendar:Calendar):Int{
            val year = calendar.get(Calendar.YEAR)
            return year
        }

        @JvmStatic
        fun getMonth(calendar:Calendar):Int{
            val month = calendar.get(Calendar.MONTH) + 1
            return month
        }

        @JvmStatic
        fun getDay(calendar:Calendar):Int{
            val day = calendar.get(Calendar.DAY_OF_MONTH)
            return day
        }

        @JvmStatic
        fun timeStamp2Calendar(timestamp:Long):Calendar{
            val calendar = Calendar.getInstance()
            calendar.time = Date(timestamp)
            return calendar
        }

        @JvmStatic
        fun yearMonthDayStrGetIntArray(ymd:String):IntArray{
            if(ymd.trim().length!=8) return intArrayOf()
            try{
                val year = ymd.substring(0,4).toInt()
                val month = ymd.substring(4,6).toInt()
                val day = ymd.substring(6,8).toInt()
                return intArrayOf(year,month,day)
            }catch (e:Exception){
                e.printStackTrace()
            }
            return intArrayOf()
        }

        @JvmStatic
        fun yearMonthDayStr2Calendar(ymd:String):Calendar?{
            if(ymd.trim().length!=8) return null
            try{
                val year = ymd.substring(0,4).toInt()
                val month = ymd.substring(4,6).toInt()
                val day = ymd.substring(6,8).toInt()
                return yearMonthDay2Calendar(year,month,day)
            }catch (e:Exception){
                e.printStackTrace()
            }
            return null
        }

        @JvmStatic
        fun yearMonthDay2Calendar(year:Int,month:Int,day:Int):Calendar{
            val calendar = Calendar.getInstance()
            calendar.set(year,month-1,day)
            return calendar
        }


        @JvmStatic
        fun getDayOfWeekByCalendar(calendar: Calendar):String{
            val dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)
            when(dayOfWeek){
                1-> return "日"
                2-> return "一"
                3-> return "二"
                4-> return "三"
                5-> return "四"
                6-> return "五"
                7-> return "六"
            }
            return "日"
        }

        @JvmStatic
        fun getWeekOfYearByCalendar(calendar: Calendar):Int{
            val weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR)
            return weekOfYear
        }

        @JvmStatic
        fun yearMonthDay2CalendarStr(year:Int,month:Int,day:Int):String{
            var str=year.toString()
            if(month<10)
                str=str+"0"+month
            else
                str=str+month

            if(day<10)
                str=str+"0"+day
            else
                str=str+day

            return str
        }

        //if equal,return 0;if 1>2,return 1;if 1<2,return -1;if error,return -2
        @JvmStatic
        fun compareCalendarStr(calendarStr1: String, calendarStr2: String): Int {
            val cs1 = calendarStr1.trim()
            val cs2 = calendarStr2.trim()
            val flag = (cs1.length != 8 || cs2.length != 8)
            if(flag) return -2

            try{
                val year1=cs1.substring(0,4).toInt()
                val month1=cs1.substring(4,6).toInt()
                val day1=cs1.substring(6,8).toInt()
                val year2=cs2.substring(0,4).toInt()
                val month2=cs2.substring(4,6).toInt()
                val day2=cs2.substring(6,8).toInt()

                if(year1>year2){
                    return 1
                }else if(year1<year2){
                    return -1
                }else{
                    if(month1>month2){
                        return 1
                    }else if(month1<month2){
                        return -1
                    }else{
                        if(day1>day2){
                            return 1
                        }else if(day1<day2){
                            return -1
                        }else{
                            return 0
                        }
                    }
                }
            }catch (e:Exception){
                e.printStackTrace()
            }


            return -2
            //return if (flag) 0 else 1
        }

        @JvmStatic
        fun getMiddleCalendarStr(start:String,end:String):MutableList<String>{
            val cs1 = start.trim()
            val cs2 = end.trim()
            val flag = (cs1.length != 8 || cs2.length != 8)
            if(flag) return mutableListOf()

            if(start==end) return mutableListOf(start)

            try{
                val year1=cs1.substring(0,4).toInt()
                val month1=cs1.substring(4,6).toInt()
                val day1=cs1.substring(6,8).toInt()
                val year2=cs2.substring(0,4).toInt()
                val month2=cs2.substring(4,6).toInt()
                val day2=cs2.substring(6,8).toInt()

                val list = mutableListOf<String>()

                if(year1>year2) return list
                if(year1==year2 && month1>month2) return list
                if(month1==month2 && day1>day2) return list

                for(i in year1 .. year2){
                    var monthStart=month1
                    var monthEnd=12
                    if(i>year1){
                        monthStart=1
                    }
                    if(i==year2){
                        //monthStart=month2
                        monthEnd=month2
                    }
                    for(j in monthStart .. monthEnd){
                        var dayStart=day1
                        var dayEnd=31
                        if(i>year1 || j>month1){
                            dayStart=1
                        }
                        if(i==year2 && j==month2){
                            //dayStart=day2
                            dayEnd=day2
                        }
                        for(k in dayStart .. dayEnd){
                            list.add(yearMonthDay2CalendarStr(i,j,k))
                        }
                    }
                }

                return list

            }catch (e:Exception){
                e.printStackTrace()
            }

            return mutableListOf()
        }

        @JvmStatic
        fun calendarAddDay(calendar:Calendar,day:Int):Calendar{
            val newCalendar = Calendar.getInstance()
            newCalendar.time = calendar.time
            newCalendar.add(Calendar.DAY_OF_MONTH,day)
            return newCalendar
        }

        @JvmStatic
        fun diffCalendarDay(calendar1: Calendar,calendar2: Calendar):Int{
            return ((calendar1.timeInMillis - calendar2.timeInMillis) / (1000 * 3600 * 24)).toInt()
        }

        @JvmStatic
        fun getCurrentHour():Int{
            val calendar = Calendar.getInstance()
            val hour = calendar.get(Calendar.HOUR_OF_DAY)
            return hour
        }
    }
}